package com.txq.bilibili.app.fragments;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.txq.bilibili.app.Constants.Constant;
import com.txq.bilibili.app.DataStore;
import com.txq.bilibili.app.R;
import com.txq.bilibili.app.adapter.SocialGridViewAdapter;
import com.txq.bilibili.app.adapter.SocialListviewAdapter;
import com.txq.bilibili.app.interfaces.SocialFragmentInterface;
import com.txq.bilibili.app.model.AlwaysContent;
import com.txq.bilibili.app.model.RecommendContent;
import com.txq.bilibili.app.tasks.SocialTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocialFragment extends Fragment implements SocialFragmentInterface,PullToRefreshListView.OnRefreshListener2 ,Animation.AnimationListener{


    private View headView;
    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private List<AlwaysContent> alwaysList;
    private ArrayList<RecommendContent> data;
    private SocialListviewAdapter listviewAdapter;
    private SocialGridViewAdapter adapterGridView;
    private boolean isDown;
    private int count;
    private String url1="http://bbs.j.cn/api/groupsV2?jcnappid=860936029351636&jcnuserid=6783defed62e98e70d3700f76707147f&pageSize=10&pageRecord=";
    private String url2="&net=wifi&v=3.1.0&longitude=4.9E-324&latitude=4.9E-324";
    private ImageView loading;
    private TextView alwawsTxt;
    private TextView recommend;
    private Animation animation;
    private FrameLayout frameLayout;

    public SocialFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_social, container, false);

       loading = (ImageView)ret.findViewById(R.id.fragment_social_refreshing_loading);
        frameLayout = (FrameLayout)ret.findViewById(R.id.fragment_isocial_img_container);

        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_animation);
        animation.setAnimationListener(this);
        loading.startAnimation(animation);

        LayoutInflater inflater1=LayoutInflater.from(getActivity());
        headView = inflater1.inflate(R.layout.fragment_social_head_view,null);

        pullToRefreshListView = (PullToRefreshListView)ret.findViewById(R.id.fragment_social_pull_to_refresh);

        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);

        listView = pullToRefreshListView.getRefreshableView();

        //设置头view的显示
        GridView gridView=(GridView)headView.findViewById(R.id.fragment_social_grid_view);
        alwawsTxt = (TextView)headView.findViewById(R.id.fragment_social_head_view_always_txt);
        recommend = (TextView)headView.findViewById(R.id.fragemtn_social_hewad_view_recommend_txt);
        alwaysList = new ArrayList<AlwaysContent>();

        listView.addHeaderView(headView);

        adapterGridView = new SocialGridViewAdapter(getActivity(),alwaysList);

        gridView.setAdapter(adapterGridView);

        //刷新

        pullToRefreshListView.setReleaseLabel("松开开始刷新哦");
        pullToRefreshListView.setReleaseLabel("正在加载");




        data = new ArrayList<RecommendContent>();

        listviewAdapter = new SocialListviewAdapter(getActivity(),data);

        listView.setDividerHeight(10);
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setAdapter(listviewAdapter);

        SocialTask task=new SocialTask(this);
        task.execute(Constant.SOCIAL_FRAGMENT_URL);


        pullToRefreshListView.setOnRefreshListener(this);


        return ret;
    }




    @Override
    public void parseJson(JSONObject object) {


        pullToRefreshListView.onRefreshComplete();


        //Log.d("json..................................",object+"");

        try {

            if(!isDown) {
                alwaysList.clear();
                data.clear();
                JSONArray visited = object.getJSONArray("visited");

                for (int i = 0; i < visited.length(); i++) {

                    JSONObject object1 = visited.getJSONObject(i);
                    AlwaysContent always = new AlwaysContent();

                    always.parseJson(object1);
                    alwaysList.add(always);

                }
            }

            JSONObject recommend=object.getJSONObject("recommend");

            int count=recommend.getInt("count");
            DataStore dataStore=DataStore.getInstance();
            dataStore.setCount(count);
            String nextPageecord=recommend.getString("nextPageRecord");
            JSONArray rows=recommend.getJSONArray("rows");

            for(int i=0;i<rows.length();i++){

                JSONObject subObject=rows.getJSONObject(i);
                RecommendContent recommendContent=new RecommendContent();
                recommendContent.parseJson(subObject);

                data.add(recommendContent);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }


        loading.setVisibility(View.INVISIBLE);
        frameLayout.setVisibility(View.INVISIBLE);
        alwawsTxt.setVisibility(View.VISIBLE);
        recommend.setVisibility(View.VISIBLE);
        listviewAdapter.notifyDataSetChanged();
        adapterGridView.notifyDataSetChanged();


    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

        isDown=false;
        count=0;
        frameLayout.setVisibility(View.VISIBLE);
        loading.setVisibility(View.VISIBLE);
        loading.startAnimation(animation);

        SocialTask task=new SocialTask(this);
        task.execute(Constant.SOCIAL_FRAGMENT_URL);


    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {


        DataStore dataStore=DataStore.getInstance();
        int countSum=dataStore.getCount();
        if(count<(countSum+10)%10) {

            frameLayout.setVisibility(View.VISIBLE);
            loading.setVisibility(View.VISIBLE);
             loading.startAnimation(animation);
            SocialTask task = new SocialTask(this);
            String url = url1 + count + url2;
            task.execute(url);
            isDown = true;
            count++;


        }
    }

    @Override
    public void onAnimationStart(Animation animation) {


    }

    @Override
    public void onAnimationEnd(Animation animation) {


    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}
